package org.gp.Interfaz;

import java.awt.EventQueue;

import javax.swing.JDialog;
import java.awt.BorderLayout;
import java.util.Iterator;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JButton;

import org.gp.tringes.GestorIGInicio;
import org.gp.tringes.GestorIGInscribirEnCarrera;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


public class IGInscribirEnCarrera extends JDialog {
	
	private Integer idClub;
	
	private JPanel panel;
	
	private JPanel panel_1;
	private JLabel lblCamp;
	private JComboBox<String> comboCamp;
	private JLabel lblCarr;
	private JComboBox<String> comboCarr;
	
	private JPanel panel_2;
	private JButton btnAceptar;
	private JButton btnCancelar;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					IGInscribirEnCarrera dialog = new IGInscribirEnCarrera(0);
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
					dialog.setModal(true);
					dialog.setResizable(false);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public IGInscribirEnCarrera(Integer pIdClub) {
		
		idClub = pIdClub;
		
		//ID-168
		setTitle(GestorIGInicio.getGestorIGInicio().getTexto(168));
		setBounds(100, 100, 300, 223);
		
		getContentPane().setLayout(new BorderLayout(0, 0));
		
		
		getContentPane().add(getPanel(), BorderLayout.NORTH);
		getContentPane().add(getPanel_1(), BorderLayout.CENTER);
		getContentPane().add(getPanel_2(), BorderLayout.SOUTH);
	}

	private JPanel getPanel(){
		if(panel == null){
			panel = new JPanel();
		}
		return panel;
	}
	
	private JPanel getPanel_1(){
		if(panel_1 == null){
			panel_1 = new JPanel();
			panel_1.setLayout(null);
			
			panel_1.add(getLblCamp());
			panel_1.add(getComboCamp());
			panel_1.add(getLblCarr());
			panel_1.add(getComboCarr());
			
		}
		return panel_1;
	}
	private JLabel getLblCamp(){
		if(lblCamp == null){
			//ID-169
			lblCamp = new JLabel(GestorIGInicio.getGestorIGInicio().getTexto(169));
			lblCamp.setHorizontalAlignment(SwingConstants.CENTER);
			lblCamp.setBounds(18, 15, 250, 15);
		}
		return lblCamp;
	}
	private JComboBox getComboCamp(){
		if(comboCamp == null){
			comboCamp = new JComboBox();
			comboCamp.setBounds(18, 45, 250, 25);
			Iterator<String> lista = GestorIGInscribirEnCarrera.getGestorIGInscribirEnCarrera().getCampeonatos();
			while(lista.hasNext()){
				String dato = lista.next();
				comboCamp.addItem(dato);
			}
			
			comboCamp.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					comboCarr.removeAllItems();
					Iterator<String> listaCarr = GestorIGInscribirEnCarrera.getGestorIGInscribirEnCarrera().getCarreras((String)comboCamp.getSelectedItem());
					while(listaCarr.hasNext()){
						String dato = listaCarr.next();
						comboCarr.addItem(dato);
					}
					if(comboCarr.getItemCount() != 0){
						comboCarr.setSelectedIndex(0);
						comboCarr.setEnabled(true);
					}else{
						comboCarr.setEnabled(false);
						btnAceptar.setEnabled(false);
					}
				}
			});
		}
		return comboCamp;
	}
	private JLabel getLblCarr(){
		if(lblCarr == null){
			//ID-170
			lblCarr = new JLabel(GestorIGInicio.getGestorIGInicio().getTexto(170));
			lblCarr.setHorizontalAlignment(SwingConstants.CENTER);
			lblCarr.setBounds(18, 85, 250, 15);
		}
		return lblCarr;
	}
	private JComboBox getComboCarr(){
		if(comboCarr == null){
			comboCarr = new JComboBox();
			comboCarr.setEnabled(false);
			comboCarr.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(comboCarr.getSelectedIndex() != -1){
						btnAceptar.setEnabled(true);
					}
				}
			});
			comboCarr.setBounds(18, 115, 250, 25);
			
		}
		return comboCarr;
	}
	
	private JPanel getPanel_2(){
		if(panel_2 == null){
			panel_2 = new JPanel();
			panel_2.add(getBtnAceptar());
			panel_2.add(getBtnCancelar());
		}
		return panel_2;
	}
	private JButton getBtnAceptar(){
		if(btnAceptar == null){
			//ID-2
			btnAceptar = new JButton(GestorIGInicio.getGestorIGInicio().getTexto(2));
			btnAceptar.setEnabled(false);
			btnAceptar.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					if(btnAceptar.isEnabled()){
						if(comboCarr.getSelectedIndex() != -1){
							//ID-171 y 4
							int opcion = JOptionPane.showConfirmDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(171), GestorIGInicio.getGestorIGInicio().getTexto(4), JOptionPane.YES_NO_OPTION);
							if(opcion == 0){
								setVisible(false);
								JDialog espera = new IGEspera();
								espera.setLocationRelativeTo(null);
								espera.setVisible(true);
								int idCarrera=GestorIGInscribirEnCarrera.getGestorIGInscribirEnCarrera().getIdCarrera((String)comboCarr.getSelectedItem());
								JDialog configCarrera = new IGConfigCarrera(idClub, idCarrera);
								configCarrera.setResizable(false);
								configCarrera.setModal(true);
								configCarrera.setLocationRelativeTo(null);
								espera.dispose();
								configCarrera.setVisible(true);
								setVisible(true);
							}
						}
					}
				}
			});
		}
		return btnAceptar;
	}
	private JButton getBtnCancelar(){
		if(btnCancelar == null){
			//ID-8
			btnCancelar = new JButton(GestorIGInicio.getGestorIGInicio().getTexto(8));
			btnCancelar.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					dispose();
				}
			});
		}
		return btnCancelar;
	}
}
